﻿Imports System.ComponentModel
Imports DTO
Imports System.Data.SqlClient

Public Class HeThongDAO

    Shared Function layDSHeThong(tu As String, dem As String) As BindingList(Of NhatKyDTO)
        Dim dsHeThong As New BindingList(Of NhatKyDTO)
        Dim cn As SqlConnection = CKetNoi.Connect
        cn.Open()
        Dim cmd As New SqlCommand()
        cmd.CommandText = "usp_layDsNhatKy"
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Connection = cn

        Dim para As SqlParameter
        para = New SqlParameter

        para = New SqlParameter("tu", SqlDbType.DateTime)
        para.Direction = ParameterDirection.Input
        para.Value = tu
        cmd.Parameters.Add(para)

        para = New SqlParameter("den", SqlDbType.DateTime)
        para.Direction = ParameterDirection.Input
        para.Value = dem
        cmd.Parameters.Add(para)


        Dim read As SqlDataReader = cmd.ExecuteReader
        While read.Read
            Dim nk As New NhatKyDTO
            nk.Ma = read("Ma")
            nk.MaNguoiDung = read("MaNguoiDung")
            nk.MayTinh = read("MayTinh")
            nk.ThoiGian = read("ThoiGian")
            nk.HanhDong = read("HanhDong")
            nk.DoiTuong = read("DoiTuong")
            nk.ChucNang = read("ChucNang")
            dsHeThong.Add(nk)
        End While
        cn.Close()
        Return dsHeThong
    End Function

    Public Shared Function themNhatKy(nk As NhatKyDTO) As Integer

        Dim dsVal As New List(Of SqlParameter)
        Dim para As SqlParameter
        para = New SqlParameter

        para = New SqlParameter("MaNguoiDung", SqlDbType.VarChar, 7)
        para.Direction = ParameterDirection.Input
        para.Value = nk.MaNguoiDung
        dsVal.Add(para)

        para = New SqlParameter("MayTinh", SqlDbType.NText)
        para.Direction = ParameterDirection.Input
        para.Value = nk.MayTinh
        dsVal.Add(para)

        para = New SqlParameter("ThoiGian", SqlDbType.DateTime)
        para.Direction = ParameterDirection.Input
        para.Value = nk.ThoiGian
        dsVal.Add(para)


        para = New SqlParameter("ChucNang", SqlDbType.NText)
        para.Direction = ParameterDirection.Input
        para.Value = nk.ChucNang
        dsVal.Add(para)

        para = New SqlParameter("HanhDong", SqlDbType.NText)
        para.Direction = ParameterDirection.Input
        para.Value = nk.HanhDong
        dsVal.Add(para)


        para = New SqlParameter("DoiTuong", SqlDbType.NText)
        para.Direction = ParameterDirection.Input
        para.Value = nk.DoiTuong
        dsVal.Add(para)

        Dim kq As Integer = CKetNoi.ExecQueryStoreProc("usp_themNhatKy", dsVal)
        Return kq
    End Function

    Shared Function layTenSever() As String
        Dim dsHeThong As New BindingList(Of NhatKyDTO)
        dim tenSever As String= ""
        Dim cn As SqlConnection = CKetNoi.Connect
        cn.Open()
        Dim cmd As New SqlCommand()
        cmd.CommandText = "usp_layTenSever"
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Connection = cn
        Dim reader As SqlDataReader = cmd.ExecuteReader
        If reader.Read Then
            tenSever = reader("name")
        End If
        cn.Close()
        Return tenSever
    End Function

    Shared Function layDataBase() As String
        Dim dsHeThong As New BindingList(Of NhatKyDTO)
        Dim tenSever As String = ""
        Dim cn As SqlConnection = CKetNoi.Connect
        cn.Open()
        Dim cmd As New SqlCommand()
        cmd.CommandText = "usp_LayTenDatabase"
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Connection = cn
        Dim reader As SqlDataReader = cmd.ExecuteReader
        If reader.Read Then
            tenSever = reader("DatabaseName")
        End If
        cn.Close()
        Return tenSever
    End Function

End Class
